/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package op;

import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;

/**
 *
 * @author Jônatas
 */
public class QuebraStringVoip {

    private static Map<String, Integer> attributes;

    public static void setAttributes(Map<String, Integer> attr) {
        attributes = attr;
    }

    public static EntradaAbstrata quebraString(HSSFRow row) {

        if (row.getPhysicalNumberOfCells() < 6) {
            return null;
        }

        EntradaAbstrata ligacao = new Ligacao();

        // nao importa
        HSSFCell cell = row.getCell(0);

        if (cell == null) {
            return null;
        }

        ligacao.setNumeroMisterioso(getStringCellValue(cell));

        // data e hora
        cell = row.getCell(1);

        if (cell == null) {
            return null;
        }

        Date date = null;

        if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            date = cell.getDateCellValue();
        } else {
            String[] temp = getStringCellValue(cell).trim().split(" ");
            date = EntradaAbstrata.criaDate(temp[0], temp[1]);
        }

        ligacao.setDataOrigemLigacao(date);

        //origem
        cell = row.getCell(2);

        if (cell == null) {
            return null;
        }

        String origemStr = getStringCellValue(cell);

        Telefone origem = null;

        Matcher matchFoneDDD = Pattern.compile("[0-9]{10}").matcher(origemStr);
        if (matchFoneDDD.find()) {
            origem = new Telefone(origemStr.substring(0, 2), origemStr.substring(2, 6),
                    origemStr.substring(6));
        } else {
            origem = new Telefone(origemStr, true);
        }

        ligacao.setNumeroDeOrigem(origem);

        //destino
        cell = row.getCell(3);

        if (cell == null) {
            return null;
        }

        String destinoStr = getStringCellValue(cell);

        Telefone destino = null;

        Matcher matchFoneDDI = Pattern.compile("[0-9]{12}").matcher(destinoStr);
        if (matchFoneDDI.find()) {
            destino = new Telefone(destinoStr);
        } else {
            destino = new Telefone(destinoStr, true);
        }

        ligacao.setDestino(destino);

        //Local
        cell = row.getCell(4);

        if (cell == null) {
            return null;
        }

        String local = getStringCellValue(cell);

        ligacao.setLocalDaLigacao(local);

        //duracao
        cell = row.getCell(5);

        if (cell == null) {
            return null;
        }

        String duracaoStr = null;

        if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            date = cell.getDateCellValue();

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            int mm = calendar.get(Calendar.HOUR_OF_DAY);

            String minute = "" + mm;
            String second = "" + calendar.get(Calendar.MINUTE);
            if (second.length() == 1) {
                second = "0" + second;
            }

            duracaoStr = "" + minute + ":";
            duracaoStr += "" + second;

        } else {
            duracaoStr = getStringCellValue(cell);
        }

        ligacao.setDuracaoLigacaoSegundos(getLigacaoSegundos(duracaoStr.trim()));

        //valor
        cell = row.getCell(6);

        if (cell == null) {
            return null;
        }

        if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            ligacao.setCusto(cell.getNumericCellValue());
        } else {
            ligacao.setCusto(Double.parseDouble(getStringCellValue(cell).
                    replace(',', '.')));
        }

        ligacao.setPlanoLigacao("");
        ligacao.setTipoDeLigacao("");

        return ligacao;
    }

    private static Integer getLigacaoSegundos(String duracao) {

        String mm = duracao.substring(0, duracao.indexOf(":"));
        String ss = duracao.substring(duracao.indexOf(":") + 1);

        int min = Integer.parseInt(mm);

        return ((min * 60) + Integer.parseInt(ss));


    }

    private static String getStringCellValue(HSSFCell cell) {
        if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
            return Boolean.toString(cell.getBooleanCellValue());
        }
        if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
            return cell.getCellFormula();
        }
        if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            NumberFormat nf = NumberFormat.getIntegerInstance();
            nf.setParseIntegerOnly(false);
            nf.setGroupingUsed(false);
            return nf.format(cell.getNumericCellValue());
        }
        if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            return cell.getStringCellValue();
        }

        return "";
    }
}
